﻿/*
*	冰火家族萬能NPC
*
*
*
*
*
*/
var x = "#fEffect/CharacterEff/1112949/0/0#";//高音譜記號
var kk = "#fEffect/BasicEff/MainNotice/Boss/Default/0#";//BOSS
var c = "#fEffect/CharacterEff/1032063/0/0#";//"#fUI/UIMiniGame/starPlanetRPS/heart#";//愛心
var cmark = "#fUI/UIPVP/ChampionMark/1#";//愛心
var lb = "#fEffect/ItemEff/1112811/0/0#";//藍色小光
var b = "#fEffect/CharacterEff/Shaman/1#";//超萌熊熊
var cat = "#fEffect/CharacterEff/Shaman/4#";//超萌喵咪
var leaf = "#fUI/Basic.img/Cursor/18/0#";
var status;
var sele1;
var scrollsrc;
var scrolls;
var opens = 2;

var scrollstar;
var targetItem;

var menuList = Array(
        Array(lb, "購買星岩魔方", 0, true),
		Array(lb, "洗星岩(單個-需魔方)", 1, true),
		Array(lb, "洗星岩(多個)", 2, true),
		Array(lb, "鑲嵌星石", 3, true)
		);
function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {

    if (mode == 0) {
	cm.dispose();
	return;
    } else if (mode == 1){
	status++;
    } else {
	status--;
    }

    switch (status) {
        case 0: 
			text = "\t  "+c+"#fn华文行楷##fs30##e#k #b星岩系統 " +c+ "\r\n#fs12##fn宋体#";
			
			var ccc = 0;
			for (var i = 0; i < menuList.length; i++) {
				if(ccc%2 == 0)
					text +="\r\n   ";
				if (menuList[i][3]) { // 如果允许显示
					text += "#d#L" + menuList[i][2] + "#" + menuList[i][0] + menuList[i][1] + "#l";
					ccc++;
				}
			}
			text += "\r\n\r\n\t\t   #L999#" + leaf +"#fn华文行楷##fs22##e#r回上一頁#l"
			text += "\r\n\r\n  " +c+c+c+c+c+c;
			cm.sendSimple(text);
            break;
        case 1: //
			sele1 = selection;
			switch (selection){
				case 0://裝備痕跡
					if(cm.getPlayer().getCSPoints(2) < 700){
						cm.sendOk("您沒有足夠的抵用券!");
						cm.dispose();
						return;
					}
					cm.sendGetNumber("你選擇的商品為#b#v5750002#售價為：700 抵用券/顆\r\n請輸入您要購買的數量",1,1,(cm.getPlayer().getCSPoints(2)/700));
					break;
				case 1://洗星石 單科 5750002
					if(!cm.haveItem(5750002, 1)){
						cm.sendOk("您沒有足夠的星岩方塊!");
						cm.dispose();
						return;
					}
					if(cm.getPlayer().getCSPoints(2) < 700){
						cm.sendOk("您沒有足夠的抵用券!");
						cm.dispose();
						return;
					}
					var inventoryType = 3;
					text = "#e經過篩選，以下為所有星岩#n\r\n#r進行一次將消耗700抵用券。\r\n\r\n#b";
					var list = cm.getInventory(inventoryType).list();
					var itemList = list.iterator();
					newItemList = new Array();
					while (itemList.hasNext()) {
						var item = itemList.next();
						var eqp = cm.getInvEquip(item.getPosition());
						if((item.getItemId() % 3060000) < 10000){
							newItemList[item.getPosition()] = item.getItemId();

						}else
							continue;
					}
					var xx = 0;
                    for (var key in newItemList) {
                        xx++;
                        text += "#L" + key + "##v" + newItemList[key] + "#  #z"+newItemList[key]+"# #l\r\n";
                    }
					if (xx <= 0) {
						text = "您沒有可以重製的星岩!"
						cm.sendPrev(text);
                    }else{
						cm.sendSimple(text);
					}
					break;
				case 2://洗星石 多科
					text = "請問您要洗哪個等級的星岩?\r\n#rB級以上星岩需要搭配#z2430760#\r\n#e進行1次合成將消耗700抵用券\r\n\r\n";
					text += "#r#L4#S級#b 星岩#l\r\n";
					text += "#r#L3#A級#b 星岩#l\r\n";
					text += "#r#L2#B級#b 星岩#l\r\n";
					text += "#r#L1#C級#b 星岩#l\r\n";
					text += "#r#L0#D級#b 星岩#l\r\n";
					cm.sendSimple(text);
					break;
				case 3:
					var inventoryType = 3;
					text = "請將要鑲嵌的道具放在裝備欄#r第一格#k\r\n#e經過篩選，以下為所有星岩#n\r\n";
					var list = cm.getInventory(inventoryType).list();
					var itemList = list.iterator();
					newItemList = new Array();
					while (itemList.hasNext()) {
						var item = itemList.next();
						var eqp = cm.getInvEquip(item.getPosition());
						if((item.getItemId() % 3060000) < 10000){
							newItemList[item.getPosition()] = item.getItemId();

						}else
							continue;
					}
					var xx = 0;
                    for (var key in newItemList) {
                        xx++;
                        text += "#L" + key + "##v" + newItemList[key] + "#  #z"+newItemList[key]+"# #l\r\n";
                    }
					if (xx <= 0) {
						text = "您沒有可以重製的星岩!"
						cm.sendPrev(text);
                    }else{
						cm.sendSimple(text);
					}
					break;
				case 999://回上一頁
					cm.dispose();
					cm.openNpc(1052253, 1);
					break;
				default:
					cm.sendOk(menuList[selection][1] + " 正在籌備中!敬請期待。");
					cm.dispose();
					break;
			}
            break;
		case 2:
			if(sele1 == 0){
				if(!cm.canHold(5750002, selection)){
					cm.sendOk("您的背包空間不足!");
					cm.dispose();
				}
				cm.gainNX(2,-700 * selection);
				cm.gainItem(5750002, selection);
				cm.sendOk("購買成功!");
				cm.dispose();
			}else if(sele1 == 1){
				cm.gainNX(2,-700);
				cm.gainItem(5750002, -1);
				cm.useNebuliteCube(selection);
				cm.sendOk("成功!!");
				status = -1;
			}else if(sele1 == 2){
				if(!cm.haveItem(2430760, 1) && selection >= 2){
					cm.sendOk("您沒有足夠的星岩魔方碎片!");
					cm.dispose();
					return;
				}
				if(cm.getPlayer().getCSPoints(2) < 700){
					cm.sendOk("您沒有足夠的抵用券!");
					cm.dispose();
					return;
				}
				var fuck = 0;
				var inventoryType = 3;
					var list = cm.getInventory(inventoryType).list();
					var itemList = list.iterator();
					newItemList = new Array();
					while (itemList.hasNext()) {
						var item = itemList.next();
						var eqp = cm.getInvEquip(item.getPosition());
						if((item.getItemId() % 3060000) < 10000){
							if(cm.getNubGrade(item.getItemId()) == selection)
								newItemList[item.getPosition()] = item.getQuantity();
						}else
							continue;
					}
					var xx = 0;
					var src1 = 0;
                    for (var key in newItemList) {
						xx += newItemList[key];
						if(newItemList[key] >= 2){
							var temp = newItemList[key];
							while(temp >= 2){
								temp-=2;
								if(!cm.haveItem(2430760, 1) && selection >= 2){
									cm.sendOk("部分合成成功!\r\n#r您沒有足夠的星岩魔方碎片!");
									cm.dispose();
									return;
								}
								if(cm.getPlayer().getCSPoints(2) < 700){
									cm.sendOk("部分合成成功!\r\n#r您沒有足夠的抵用券!");
									cm.dispose();
									return;
								}
								if(!cm.useNebuliteCubeMuti(key, key)){
									cm.sendOk("部分合成成功!\r\n#r背包空間不足!");
									cm.dispose();
									return;
								}
								cm.gainNX(2,-700);
								if(selection >= 2)
									cm.gainItem(5750002, -1);
							}
							if(key == 1 && src1 == 0)
								src1 = key;
							else if(key == 1 && src1 != 0){
								if(!cm.haveItem(2430760, 1) && selection >= 2){
									cm.sendOk("部分合成成功!\r\n#r您沒有足夠的星岩魔方碎片!");
									cm.dispose();
									return;
								}
								if(cm.getPlayer().getCSPoints(2) < 700){
									cm.sendOk("部分合成成功!\r\n#r您沒有足夠的抵用券!");
									cm.dispose();
									return;
								}
								if(!cm.useNebuliteCubeMuti(src1, key)){
									cm.sendOk("部分合成成功!\r\n#r背包空間不足!");
									cm.dispose();
									return;
								}
								
								cm.gainNX(2,-700);
								if(selection >= 2)
									cm.gainItem(5750002, -1);
								src1 = 0
							}
						}else{
							if(src1 == 0)
								src1 = key;
							else{
								if(!cm.haveItem(2430760, 1) && selection >= 2){
									cm.sendOk("部分合成成功!\r\n#r您沒有足夠的星岩魔方碎片!");
									cm.dispose();
								}
								if(cm.getPlayer().getCSPoints(2) < 700){
									cm.sendOk("部分合成成功!\r\n#r您沒有足夠的抵用券!");
									cm.dispose();
								}if(!cm.useNebuliteCubeMuti(src1, key)){
									cm.sendOk("部分合成成功!\r\n#r背包空間不足!");
									cm.dispose();
									return;
								}
								
								cm.gainNX(2,-700);
								if(selection >= 2)
									cm.gainItem(5750002, -1);
								src1 = 0
							}
						}
                    }
					if (xx <= 1) {
						text = "您沒有可進行融合的星石!"
						cm.sendOk(text);
						status = -1;
                    }else{
						cm.sendOk("全部融合成功!!");
						xx = 0;
						status = -1;
					}
			}else if(sele1 == 3){
				scrollstar = cm.getInventory(3).getItem(selection);
				targetItem = cm.getInventory(1).getItem(1);
				if(targetItem == null || scrollstar == null){
					cm.sendOk("您未將道具放置於第一格!");
					cm.dispose();
					return;
				}
				if(targetItem == null){
					cm.sendOk("獲取星石錯誤，請聯繫管理員!");
					cm.dispose();
					return;
				}
				var ii = cm.getItemInfo();
				var strInfo = ii.getSocketInfo(scrollstar.getItemId());
				if(strInfo == null){
					cm.sendOk("發生錯誤，請聯繫管理員!");
					cm.dispose();
					return;
				}else if(!cm.canSocket(strInfo,targetItem.getItemId())){
					cm.sendOk("此道具無法使用此星石!");
					cm.dispose();
					return;
				}
				if(targetItem.getSocket1() <= 0){
					cm.sendOk("鑲嵌第一個星石請直接雙擊星石!");
					cm.dispose();
					return;
				}else if(targetItem.getSocket2() == -1){
					cm.sendOk("請先打第二孔才能鑲嵌!");
					cm.dispose();
					return;
				}else if(targetItem.getSocket3() == -1 &&targetItem.getSocket2() >0){
					cm.sendOk("請先打第三孔才能鑲嵌!");
					cm.dispose();
					return;
				}
				if(targetItem.getSocket3() == 0 &&targetItem.getSocket2() >0)
					opens = 3;
				
				text = "您選擇的道具為 #r#v" + targetItem.getItemId() + "##z" + targetItem.getItemId() + "#\r\n#k您選擇的星石為 #r#v" + scrollstar.getItemId() + "##z" + scrollstar.getItemId() + "##k";
				text += "\r\n您即將鑲嵌在第 #r" + opens + " #k格 孔位";
				cm.sendYesNo(text);
			}
			break;
		case 3:
			if(sele1 == 3){
				if(opens == 2){
					targetItem.setSocket2(scrollstar.getItemId());
					cm.getPlayer().forceUpdateItem(targetItem);
					cm.gainItem(scrollstar.getItemId(),-1);
					cm.sendOk("鑲嵌第二個位置成功!");
					cm.dispose();
					return;
				}else if(opens == 3){
					targetItem.setSocket3(scrollstar.getItemId());
					cm.getPlayer().forceUpdateItem(targetItem);
					cm.gainItem(scrollstar.getItemId(),-1);
					cm.sendOk("鑲嵌第三個位置成功!");
					cm.dispose();
					return;
				}else{
					cm.sendOk("發生錯誤，請聯繫管理員!");
					cm.dispose();
					return;
				}
			}
			break;
        default:
			cm.sendOk("發生錯誤，請聯繫管理員!");
			cm.dispose();
			break;
    }
}
